const {
	Msg,
	Paths
} = require('../tools/message.js');

const express = require('express');
const {
	Msg,
	Paths
} = require('../tools/message.js');
const mysqlObj = require('../model/mysql.class.js');
const router = express.Router();

//设计路由

//根据商品id查询商品相关信息：
router.get('/goodsinfo', async (req, res) => {

	//接参(商品id)
	let goodsid = req.query.gid ? req.query.gid : '';
	let gidreg = /^\d{19}$/;
	if (goodsid == '') {
		res.send(Msg(500, '商品ID不能为空'));
		return;
	} else if (!gidreg.test(goodsid)) {
		res.send(Msg(500, '商品ID格式不正确'));
		return;
	}

	//返回的数据：
	let datas = {};

	//根据商品id查询商品相关信息：
	let sql =
		`select thired_name,second_name,first_name, g.goods_id,goods_name,goods_introduce,goods_price from goods_list as g
    left join category_first as c 
    on g.first_id=c.first_id 
    left join category_second as s 
    on g.second_id=s.second_id
    left join category_thired as t
    on g.thired_id=t.thired_id
    where g.goods_id='${goodsid}'`;
	let [err, arr] = await mysqlObj.exec(sql);
	datas.goods_info = arr;

	//根据商品id查询商品图片：
	sql = `select ()* from goods_image where goods_id='${goodsid}'`;
	[err, arr] = await mysqlObj.exec(sql);
	datas.goods_image = arr;

	//根据商品id查询商品规格：
	sql = `select * from goods_style where goods_id='${goodsid}'`;
	[err, arr] = await mysqlObj.exec(sql);
	datas.goods_style = arr;

	//根据商品id查询商品的评论数据：
	sql =
		`select eval_text,create_time,username from goods_eval as g left join member as m on g.uid=m.uid where goods_id='${goodsid}'`;
	[err, arr] = await mysqlObj.exec(sql);
	datas.goods_eval = arr;

	res.send(Msg(200, '商品详情信息查询成功', datas));
});

module.exports = router;
